<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="600" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="110px" label-position="left">
                    <el-form-item label="appId" prop="appId">
                         <el-input type="text" v-model="form.appId" placeholder="appId" clearable></el-input>
                    </el-form-item>
                    <el-form-item label="名称" prop="name">
                         <el-input type="text" v-model="form.name" placeholder="名称" clearable></el-input>
                    </el-form-item>
                    <el-form-item label="类型" prop="type">
                        <sc-select placeholder="请选择类型"  v-model="form.type" dic="wechatAccount" clearable filterable style="width: 100%;"></sc-select>
                    </el-form-item>
                    <el-form-item label="appSecret" prop="appSecret">
                         <el-input type="text" v-model="form.appSecret" placeholder="appSecret" clearable></el-input>
                    </el-form-item>
                    <el-form-item label="token" prop="token">
                         <el-input type="text" v-model="form.token" placeholder="token" clearable></el-input>
                    </el-form-item>
                    <el-form-item label="aesKey" prop="aesKey">
                         <el-input type="text" v-model="form.aesKey" placeholder="aesKey" clearable></el-input>
                    </el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaving" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaving: false,
				//表单数据
                form: {
                  appId: "",
                  name: "",
                  appSecret: "",
                  token: "",
                  aesKey: "",
                  type: "",
                  },
              //验证规则
              rules: {
                appId: [
                  {required: true, message: '请输入appId'}
                ],
                name: [
                  {required: true, message: '请输入名称'}
                ],
                appSecret: [
                  {required: true, message: '请输入appSecret'}
                ],
                token: [
                  {required: true, message: '请输入token'}
                ],
                type: [
                  {required: true, message: '请输入类型'}
                ],
                },
                }
		},
		mounted() {
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaving = true;
						let res;
						if(this.mode === 'edit')res = await this.$API.wechat.wechatAccount.edit.post(this.form)
						else if(this.mode === 'add')res = await this.$API.wechat.wechatAccount.save.post(this.form)
						this.isSaving = false;
						if(res.success){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}
					}
				})
			},
			//表单注入数据
			setData(data){
			    Object.assign(this.form, data)
			}
		}
	}
</script>

<style>
</style>